//package com.xzzz.irda.guardcat.server.repos.elasticsearch;
//
//import com.xzzz.common.base.util.json.JsonUtil;
//import com.xzzz.expand.es.core.EsCurdManager;
//import com.xzzz.irda.guardcat.server.app.pojo.MachineInfo;
//import com.xzzz.irda.guardcat.server.metric.MetricRepository;
//import com.xzzz.irda.guardcat.server.metric.gc.GcInfo;
//import lombok.extern.slf4j.Slf4j;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.SearchHit;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.sort.SortOrder;
//import org.springframework.beans.factory.annotation.Autowired;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * @author xzzz
// */
//@Slf4j
//public class GcRepositoryInEs extends MetricRepository<GcInfo> {
//
//    private static final String INDEX_NAME = "index_guardcat_gc";
//    public static final String PS_SCAVENGE = "PS Scavenge";
//    public static final String PS_MARKSWEEP = "PS MarkSweep";
//
//    @Autowired
//    public EsCurdManager esCurdManager;
//
//    @Override
//    public void save(MachineInfo machineInfo, GcInfo gcInfo) {
//        gcInfo.setAppName(machineInfo.getAppName());
//        gcInfo.setMachineId(machineInfo.getMachineId());
//        gcInfo.setId(machineInfo.getMachineId() + "-" + gcInfo.getGcId());
//        esCurdManager.add(INDEX_NAME, gcInfo);
//    }
//
//    @Override
//    public void expire() {
//
//    }
//
//    @Override
//    public GcInfo getByMachineId(String machineId) {
//        return null;
//
//    }
//
//    /**
//     *
//     * @param machineId 机器的 id
//     * @return
//     */
//    @Override
//    public List<GcInfo> listByMachineId(String machineId) {
//        SearchSourceBuilder searchSource = new SearchSourceBuilder()
//            .size(500)
//            .sort("startTime", SortOrder.DESC)
//            .fetchSource(new String[]{"startTime", "duration", "name"}, null);
//
//        searchSource.query(QueryBuilders.termQuery("machineId", machineId));
//
//        SearchResponse resp = esCurdManager.search(INDEX_NAME, searchSource);
//
//        List<GcInfo> list = new ArrayList<>(resp.getHits().getHits().length);
//        for (SearchHit hit : resp.getHits()) {
//            GcInfo memoryEs = JsonUtil.toObj(hit.getSourceAsString(), GcInfo.class);
//            list.add(memoryEs);
//        }
//        return list;
//    }
//
//    @Override
//    public List<GcInfo> list(MachineInfo machineInfo, GcInfo gcInfo, Map<String,Object> args) {
//        List<GcInfo> list = new ArrayList<>(2);
//
//        // young gc
//        SearchSourceBuilder searchSourceYoung = new SearchSourceBuilder()
//            .size(1)
//            .sort("startTime", SortOrder.DESC)
//            .fetchSource(new String[]{"totalGcCount", "totalGcTime", "name"}, null);
//
//        searchSourceYoung
//                .query(
//                    QueryBuilders.boolQuery()
//                    .must(QueryBuilders.termQuery("machineId", machineInfo.getMachineId()))
//                    .must(QueryBuilders.termQuery("name", "PS Scavenge"))
//                );
//        SearchResponse resp = esCurdManager.search(INDEX_NAME, searchSourceYoung);
//
//        for (SearchHit hit : resp.getHits()) {
//            GcInfo memoryEs = JsonUtil.toObj(hit.getSourceAsString(), GcInfo.class);
//            list.add(memoryEs);
//        }
//
//        SearchSourceBuilder searchSourceFull = new SearchSourceBuilder()
//            .size(1)
//            .sort("startTime", SortOrder.DESC)
//            .fetchSource(new String[]{"totalGcCount", "totalGcTime", "name"},null);
//
//        searchSourceFull
//                .query(
//                    QueryBuilders.boolQuery()
//                    .must(QueryBuilders.termQuery("machineId", machineInfo.getMachineId()))
//                    .must(QueryBuilders.termQuery("name", "PS MarkSweep"))
//                );
//        SearchResponse respFull = esCurdManager.search(INDEX_NAME, searchSourceFull);
//
//        for (SearchHit hit : respFull.getHits()) {
//            GcInfo memoryEs = JsonUtil.toObj(hit.getSourceAsString(), GcInfo.class);
//            list.add(memoryEs);
//        }
//
//        return list;
//    }
//}
